// JavaScript Documentvar lst_countries_all = false;
var lst_countries = false;
var lst_regions = false;
var lst_codes = false;
var lst_languages = false;
var users_subs = {};
var users_names = {};
var users_parents = {};
var users_indexes = {};
var less_20_clr = '';
var current_uid = 0;
var current_ui = 0;
var current_uname = '';
var loaded_datagrid = '0';
var loaded_search = '';
var loaded_search_field = '';
var loaded_members_tree = '';
var loaded_members_ptree = 0;
var loaded_members_edit = 0;
var loaded_advances = {};
var loaded_notes = {};
var default_country = 50;
var default_region = 4312;
var default_language = 570;
$(function() {
    $('#loaded_datagrid').val(loaded_datagrid);
    url = 'usfl_ud';
	 $.ajax({
	   	 url:baselocation+"/members/initMembersTree/",
	   	 type: 'get',
	   	 dataType: 'json',
	        success: function(result) {
	        	var	data = result;					
	            users_first = data.usfl;
	            users_names[0] = data.ud[0];
	            users_names[user_perms['id']] = data.ud[0];
	            initMembersTree();
	            initMembers(1,1);			
	        }
	    });
	 loadMembers();

});
function initMembers(pageNumber,pageSize) {
    $('#members_search').searchbox({
        width: 300,
        searcher: function(value, name) {
        	loadingShow();
	     	 $.ajax({
   	    	 url:baselocation+"/members/getMembersJson/",
   	    	 dataType: 'json',
   	    	 data:{s:value,f:name,p:$('#loaded_datagrid').val()},
   	         success: function(result) {
   	        	 data = result;
    	        $("#members").datagrid("loadData",data);
   	         	}
   	        })
   	      setTimeout(function(){
   	 		loadingHide();
   	 	},500);
            $('#members').datagrid('load', {
                s: value,
                f: name
            }); 
            loaded_search_field = name;
            loaded_search = value;
          //  loaded_datagrid = false;
        },
        menu: '#members_search_menu',
        prompt: getTranslation('Search Member')
    });
    var loaded_per_page = fromCookie('members', 'per_page');
    if (loaded_per_page < 10) loaded_per_page = 50;
	//
	     $.ajax({
    	 url:baselocation+"/members/getMembersJson/",
    	 dataType: 'json',
    	 data:{p:$('#loaded_datagrid').val()},
         success: function(result) {
        	 var data=result;
        	 $("#members").datagrid("loadData", data);
        	 $('#member_menu').menu();
//        	    if (!checkUserPermission('members_secondline', 'view')) {
//        	        $('#members').datagrid('hideColumn', 'p');
//        	    }
//        	    if (!checkUserPermission('referers', 'view')) {
//        	        $('#members').datagrid('hideColumn', 'r');
//        	    }
        	    if (!checkUserPermission('members', 'edit')) {
        	        $('#mm_edit, .mm_edit').hide();
        	    }
        	    $('#mm_edit_parent, .mm_edit_parent').hide();
        	    if (checkUserPermission('member_parent', 'edit') || checkUserPermission('member_parent_anywhere', 'edit')) {
        	        $('#mm_edit_parent, .mm_edit_parent').show();
        	    }
        	    if (!checkUserPermission('member_user_maratory', 'edit') && !checkUserPermission('member_user_maratory', 'add')) {
        	        $('#mm_edit_lock, .mm_edit_lock').hide();
        	    }
        	    if (!checkUserPermission('members_csv')) {
        	        $('#mm_export').hide();
        	    }
        	    var pager = $('#members').datagrid('getPager');
        	    pager.pagination({
        	    	total:data.total,
        	        showPageList: false,
        	        displayMsg: '<table align="center"><tr><td align="left" valign="top" style="background-color:#cfeefb">' + '<a href="#" onclick="return false" >' + getTranslation('MMB_COLOR_1') + '</a>' + '</td><td valign="top" align="left" style="background-color:#cee5fb">' + '<a href="#" onclick="return false">' + getTranslation('MMB_COLOR_2') + '</a>' + '</td><td valign="top" align="left" style="background-color:#defcf5">' + '<a href="#" onclick="return false">' + getTranslation('MMB_COLOR_3') + '</a>' + '</td><td valign="top">' + pager.pagination.defaults.displayMsg + '</td></tr></table>',
        	        onBeforeRefresh: function() {
        	            return true;
        	        },
        	        onSelectPage: function(pageNum,pageSize) {
        	        	loadingShow();
        	     	   $.ajax({
  	        	    	 url:baselocation+"/members/getMembersJson/",
  	        	    	 dataType: 'json',
  	        	    	 data:{pageNum:pageNum,'flt': $('#member_filter_form').serialize(),p:$('#loaded_datagrid').val()},
  	        	         success: function(result) {
  	        	        	 data = result;
  	         	        	$("#members").datagrid("loadData", data);   
  	        	         	}
  	        	        })
  	        	      setTimeout(function(){
  		    	 		loadingHide();
  		    	 	},500);
        	        }
        	    });
         }
     });	   
}
function loadMembers(){
 	var loaded_per_page = fromCookie('members', 'per_page');
    if (loaded_per_page < 10) loaded_per_page = 50;
   $('#members').datagrid({
        pagination: true,
        pageSize: loaded_per_page,
        pageList: ['10', '25', '50', '100'],
        dataType: 'json',
        idField: 'i',
        columns: [[{
            field: 't',
            title: getTranslation('Position'),
            width: 80,
            sortable: true,
            formatter: function(val, row, index) {
            	if(val==0){
            		return getTranslation('mmb_simple');
            	}else{
            		return getTranslation('animal_0');
            	}  
            }
        },
        {
            field: 'n',
            title: getTranslation('Name'),
            width: 240,
            sortable: true
        },
        {
            field: 'e',
            title: getTranslation('Email'),
            width: 200,
            sortable: true
        },
        {
            field: 'm',
            title: getTranslation('Mobile'),
            width: 100,
            sortable: true
        },
        {
            field: 'r',
            title: getTranslation('Referer'),
            width: 100,
            sortable: true
        },
        {
            field: 's',
            title: getTranslation('Status'),
            width: 80,
            sortable: true,
            formatter: function(val, row, index) {
                if (val == 1) return getTranslation('Active');
                else if (val == 3) return getTranslation('Removed');
                else if (val == 2) return getTranslation('Blocked');
                else return getTranslation('New');
            }
        },
        {
            field: 'd',
            title: getTranslation('Creation date'),
            width: 80,
            sortable: true
        },
//        {
//            field: 'adv',
//            title: getTranslation('Advance'),
//            width: 30,
//            sortable: true,
//            formatter: function(val, row, index) {
//                loaded_advances[row.i] = val;
//                return (val == 1) ? getTranslation('Yes') : getTranslation('No');
//            }
//        },
        {
            field: 'isl',
            title: getTranslation('adm_is_leave'),
            width: 30,
            sortable: true,
            formatter: function(val, row, index) {
                return val > 0 ? 'v': '';
            }
        }]],
        fitColumns: true,
        fit: true,
        singleSelect: true,
        toolbar: '#members_tb',
        onClickRow: function(index, row) {
			
            current_ui = index;
            current_uid = row.i;
            current_uname = row.n;
            doDetailsForm();
        },
        onRowContextMenu: function(e, index, row) {
            $('#members').datagrid('selectRow', index);
            e.preventDefault();
            $('#member_menu').menu('show', {
                left: e.pageX - 10,
                top: e.pageY - 10
            });
//            if (row.note && row.note.length > 0) {
//                $('#mm_note_edit').show();
//                $('#mm_note_remove').show();
//                $('#mm_note_add').hide();
//            } else {
//                $('#mm_note_edit').hide();
//                $('#mm_note_remove').hide();
//                $('#mm_note_add').show();
//            }
            $('#mm_note_edit').hide();
            $('#mm_note_remove').hide();
            $('#mm_note_add').hide();
//            if (checkUserPermission('member_parent_in_branch', 'edit')) {
//                if (!checkUserPermission('member_parent', 'edit') && !checkUserPermission('member_parent_anywhere', 'edit')) {
//                    if (row.pi == user_cid) {
//                        $('#mm_edit_parent, .mm_edit_parent').show();
//                    } else {
//                        $('#mm_edit_parent, .mm_edit_parent').hide();
//                    }
//                }
//            }
            current_ui = index;
            current_uid = row.i;
            current_uname = row.n;
            return false;
        },
        onLoadSuccess: function(data) {
            var columns = $('#members').datagrid('getColumnFields');
            $.each(columns,
            function(i, obj) {
//                if (typeof(data['rows']) != 'undefined' && typeof(data['rows'][0]) != 'undefined' && typeof(data['rows'][0][obj]) != 'undefined') {
//                    $('#members').datagrid('showColumn', obj);
//                } else {
//                    $('#members').datagrid('hideColumn', obj);
//                }
            	 $('#members').datagrid('showColumn', obj);
            });
            $('#members').datagrid('fitColumns');
        },
        onBeforeLoad: function(params) {
            users_indexes = {};
            loaded_notes = {};
            toCookie('members', 'per_page', params.rows);
        },
        rowStyler: function(index, row) {
            users_parents[row.i] = row.pi;
            users_subs[row.i] = row.c;
            users_names[row.i] = row.n;
            users_indexes[row.i] = index;
            loaded_notes[row.i] = row.note;
            if (index == 0) {
                less_20_clr = row['less_20_clr'];
            }
            var style = '';
            if (less_20_clr && row.t < 20) {
                style += 'color:' + less_20_clr;
            } else {
                if (row.s == 3) style += 'color:#DDDDDD;';
                if (row.s == 2) style += 'color:pink;';
                if (row.clr == 1) {
                    style += 'background-color:#cfeefb;';
                } else if (row.clr == 2) {
                    style += 'background-color:#cee5fb;';
                } else if (row.clr == 3) {
                    style += 'background-color:#defcf5;';
                } else if (row.clr == 4) {
                    if (row.color == 1) style += 'background-color:#efefef;';
                } else if (row.clr != 0) {
                    if (row.color == 1) style += 'background-color:#efefef;';
                }
            }
            return style;
        }
    });
}
function initMembersTree() {
	  //  if (!checkUserPermission('members_secondline', 'view') || !users_first || users_first.length == 0) return false;
	     var html = '<ul id="members_tree"></ul></div>';
		 $('#work_inner').layout('add', {
	        region: 'west',
	        width: 184,
	        title: getTranslation('Groups'),
	        split: true,
	        content: html
	    });
		 
	    $('#members_tree').tree({
	      //  url: '55.npl',
	        onSelect: function(node) {
	            if (node.id) {
	                if (node.id.split) {
	                    var tmp = node.id.split('_');
	                    var id = node.id;
	                    if (tmp[1] == 1) {
	                        id = tmp[0];
	                    } else {
	                        $('#members_tree').tree('expand', node.target);
	                    }
	                }
	            }
//	            var load_datagrid = false;
//	            if (node.id == '0' || node.id === '0_1' || node.id === 'b' || users_subs[id] > 0) {
//	                load_datagrid = node.id;
//	            } else if (users_first[id]) {
//	                load_datagrid = '0';
//	            } else if (users_parents[id]) {
//	                load_datagrid = users_parents[id];
//	            }
	         //   if (load_datagrid !== false && loaded_datagrid != load_datagrid) {
	          //      loaded_datagrid = load_datagrid;
	        		var load_datagrid = node.id;
	                $("#loaded_datagrid").val(load_datagrid);
	                loaded_search_field = '';
	                loaded_search = '';
	            	loadingShow();
	            	loadMembers();
	            	initMembers(1,1);
	    	      setTimeout(function(){
	    	 		loadingHide();
	    	 	},500);
	         //   }
	        },
	        onContextMenu: function(e, row) {
	            e.preventDefault();
	            $('#member_menu').menu('show', {
	                left: e.pageX - 10,
	                top: e.pageY - 10
	            });
	            current_ui = users_indexes[row.id] ? users_indexes[row.id] : 0;
	            if (current_ui) {
	                $('#members').datagrid('selectRow', current_ui);
	            }
	            current_uid = row.id;
	            current_uname = users_names[row.id] ? users_names[row.id] : row.text;
	            return false;
	        },
	        onExpand: function(node) {
	            toCookie('members_tree', node.id, 1);
	        },
	        onCollapse: function(node) {
	            toCookie('members_tree', node.id, 0);
	        },
	        loader: function(node, func1, func2) {
	            if (!node.id) return false;     
				//
			     $.ajax({
			    	 url:baselocation+"/members/getMembersTree/",
			    	 dataType: 'json',
			    	 data:{id : node.id},
			         success: function(result) {
			        	var data = result.rows;
		                   if (data) {
		                        var tmp = new Array();
		                        var with_subs = false;
		                        var new_subs = [];
		                        $.each(data,
		                        function(i, obj) {
		                            if (obj.c > 0) {
		                                with_subs = true;
		                            }
		                        });
		                        if (with_subs) {
		                            tmp.push({
		                                'id': node.id + '_0',
		                                iconCls: 'icon-mmb-first',
		                                'text': getTranslation('First Level')
		                            });
		                            tmp.push({
		                                'id': node.id + '_1',
		                                iconCls: 'icon-mmb-all',
		                                'text': getTranslation('All Levels')
		                            });
		                        }
		                        $.each(data,
		                        function(i, obj) {
		                            users_parents[i] = node.id;
		                            users_subs[i] = obj.c;
		                            users_names[i] = obj.n;
		                            if (obj.c > 0) {
		                                new_subs[i] = obj.c;
		                                name = obj.n + ' (' + obj.c + ')';
		                                tmp.push({
		                                    'id': obj.i,
		                                    'iconCls': 'icon-group',
		                                    'text': name,
		                                    'state': 'closed'
		                                });
		                            } else {
		                                name = obj.n;
		                                if (obj.t > 10) {
		                                    tmp.push({
		                                        'id': obj.i,
		                                        'iconCls': 'icon-mmb-dec',
		                                        'text': name
		                                    });
		                                } else {
		                                    tmp.push({
		                                        'id': obj.i,
		                                        'iconCls': 'icon-mmb-simple',
		                                        'text': name
		                                    });
		                                }
		                            }
		                        });
		                        func1(tmp);
		                    }
			         }
			     });
	      
	        }
	    });
	    doMembersTree();
	}
var collapsed = [];
function doDetailsForm() {
   // if (!checkUserPermission('members') && !checkUserPermission('members_firstline')) return false;
    if (!$('#member_details_' + current_uid).html()) {
        var html = '<div id="member_details_' + current_uid + '" rel="' + current_uid + '" iconCls="icon-help" title="' + current_uname + '">Loading...</div>';
        $('#member_details_area').append(html);
        $('#member_details_' + current_uid).window({
            width: 800,
            height: 500,
            collapsible: true,
            minimizable: false,
            maximizable: false,
            href: baselocation+'/members/getMemberDetail?i=' + current_uid,
            buttons: checkUserPermission('members', 'view') ? ('#member_details_buttons_' + current_uid) : '',
            onLoad: function(data) {
                if (handleState(data)) {
                    $('#member_details_' + current_uid).html(htmlDetailsForm(data));
                    $('#member_details_' + current_uid + ' a').attr('rel', current_uid);
                } else {
                    showAlert('mmb_not_found');
                }
            },
            onBeforeClose: function() {
                collapsed[$(this).attr('rel')] = 0;
            },
            onBeforeExpand: function() {
                collapsed[$(this).attr('rel')] = 0;
                $(this).window('resize', {
                    left: Math.round(window ? ($(window).width() / 2 - 300) : 300),
                    top: 60,
                    width: 800
                });
            },
            onCollapse: function() {
                collapsed[$(this).attr('rel')] = 1;
                var count = 0;
                $.each(collapsed,
                function(i, val) {
                    if (val == 1) count++;
                });
                var max_rows = Math.floor(window ? ($(window).width() / 200) : 4);
                var collapsed_row = Math.floor((count - 1) / max_rows);
                var collapsed_column = count - 1 - collapsed_row * max_rows;
                $(this).window('resize', {
                    left: collapsed_column * 200,
                    top: $(window).height() - 40 - 30 * collapsed_row,
                    width: 200
                });
            }
        });
    } else {
        $('#member_details_' + current_uid).window('open');
        if (collapsed[$(this).attr('rel')] == 1) {
            $('#member_details_' + current_uid).window('resize', {
                left: Math.round(window ? ($(window).width() / 2 - 300) : 300),
                top: 60,
                width: 800
            });
            $('#member_details_' + current_uid).window('expand');
        }
    }
}
function doClose(type, link) {
    if (type == 'member_details') {
        uid = $(link).attr('rel');
        if (!uid) uid = current_uid;
        $('#member_details_' + uid).dialog('close');
    } else if (type == 'parent') {
        $('#member_parent').dialog('close');
    } else if (type == 'advance') {
        $('#member_advance').dialog('close');
    } else if (type == 'edit') {
        $('#edit_mc').val('');
        $('#member_edit').dialog('close');
    } else if (type == 'code') {
        $('#edit_mc').val('');
        $('#member_code').dialog('close');
    } else if (type == 'block') {
        $('#block_arr_date').datebox('setValue', '');
        $('#block_note').val('');
        $('#member_block').dialog('close');
    } else {
        $('#' + type).dialog('close');
    }
}
var loaded_members_filter = 0;
function doFilterForm() {
    if (!loaded_members_filter) {
        loaded_members_filter = 1;
        var options_1 = new Array({
            'value': 0,
            'text': getTranslation('All')
        });
        $.each(lst_countries,
        function(id, value) {
            options_1.push({
                'value': id,
                'text': value
            });
        });
        var options_2 = new Array({
            'value': 0,
            'text': getTranslation('All')
        });
        if (lst_regions[default_country]) {
            $.each(lst_regions[default_country],
            function(id, value) {
                options_2.push({
                    'value': id,
                    'text': value
                });
            });
        }
        $('#filter_ic').combobox({
            value: 0,
            editable: false,
            required: true,
            multiple: false,
            data: options_1,
            onSelect: function(row) {
                var options_2 = new Array({
                    'value': 0,
                    'text': getTranslation('All')
                });
                if (lst_regions[row.value]) {
                    $.each(lst_regions[row.value],
                    function(id, value) {
                        options_2.push({
                            'value': id,
                            'text': value
                        });
                    });
                    $('#filter_ir').combobox({
                        data: options_2
                    });
                    $('#filter_ir').combobox('setValue', '');
                }
            },
            filter: function(q, row) {
                return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
            }
        });
        $('#filter_ir').combobox({
            value: 0,
            multiple: false,
            editable: false,
            data: options_2,
            required: true,
            filter: function(q, row) {
                return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
            }
        });
        $('#filter_ac').combobox({
            value: 0,
            multiple: false,
            editable: false,
            data: new Array({
                'value': 0,
                'text': getTranslation('All')
            },
            {
                'value': 1,
                'text': getTranslation('No Accounts')
            },
            {
                'value': 2,
                'text': getTranslation('Has Accounts')
            }),
            required: true,
            filter: function(q, row) {
                return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
            }
        });
        $('#filter_ps').combobox({
            value: 0,
            multiple: false,
            editable: false,
            data: new Array({
                'value': 0,
                'text': getTranslation('All')
            },
            {
                'value': 1,
                'text': getTranslation('Member')
            },
            {
                'value': 2,
                'text': getTranslation('mmb_dec')
            }),
            required: true,
            filter: function(q, row) {
                return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
            }
        });
    }
    $('#member_filter').show();
    $('#member_filter').dialog({
        width: 600,
        height: 240,
        title: getTranslation('Filter'),
        buttons: '#member_filter_buttons'
    });
}
if($('#loaded_datagrid').val()!=""){
	loaded_datagrid=$('#loaded_datagrid').val();
}
function doFilter() {
	loadingShow();
	 $.ajax({
	 url:baselocation+"/members/getMembersJson/",
	 dataType: 'json',
	 data:{'flt': $('#member_filter_form').serialize(),p:$('#loaded_datagrid').val()},
     success: function(result) {
    	 data = result;
     	$("#members").datagrid("loadData", data);  	
     	}
    })
  setTimeout(function(){
		loadingHide();
	},500);

    doClose('member_filter');
}
var saving_in_process = 0;
var bsaving_in_process = 0;
function htmlDetailsForm(data) {
    var position_title = toUsersPosition(data[42][3]);
    if(data[42][3]==10){
    	position_title=getTranslation('mmb_simple');
    }else{
    	position_title=getTranslation('animal_0');
    }
    var html = '<table height="100%" width="100%"><tr><td valign="top" align="center">';
    html += '<div style="height:390px;overflow-y:scroll;">';
    html += '<table class="datagrid-body" width="100%" height="100%">';
    html += '<tr  class="datagrid-row-selected"><th colspan="2" style="height:24px"><div class="datagrid-cell">' + getTranslation('General') + '</div></th></tr>';
    if (data[42][0]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Invite') + '</div></td><td><div class="datagrid-cell">' + data[42][0] + '</div></td></tr>';
    if (data[42][2]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('First Name') + '</div></td><td><div class="datagrid-cell">' + data[42][2] + '</div></td></tr>';
    if (data[42][1]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Last Name') + '</div></td><td><div class="datagrid-cell">' + data[42][1] + '</div></td></tr>';
    if (data[42][3]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Status') + '</div></td><td><div class="datagrid-cell">' + position_title + '</div></td></tr>';
    if (data[42][4]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Email') + '</div></td><td><div class="datagrid-cell"><a href="mailto:' + data[42][4] + '">' + data[42][4] + '</a></div></td></tr>';
    if (data[42][5]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Mobile') + '</div></td><td><div class="datagrid-cell"><a href="callto:' + data[42][5] + '">' + data[42][5] + '</a></div></td></tr>';
    if (data[42][9]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Registered') + '</div></td><td><div class="datagrid-cell">' + data[42][9] + '</div></td></tr>';
    //if (data[42][11] || data[42][12] || data[42][13] || data[42][14] || data[42][15] || data[42][16]) html += '<tr class="datagrid-row-selected"><th colspan="2"  style="height:24px"><div class="datagrid-cell">' + getTranslation('Contacts') + '</div></th></tr>';
    if (data[42][19]) {
        $.each(data[42][19],
        function(i, v) {
         //   html += '<tr class="datagrid-row-selected"><th colspan="2" style="height:24px">' + toUsersPosition(v[2]) + '</div></th></tr>';
        	html += '<tr class="datagrid-row-selected"><th colspan="2" style="height:24px">Guider</div></th></tr>';
        	if (v[1]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Name') + '</div></td><td><div class="datagrid-cell">' + v[1] + '</div></td></tr>';
            if (v[3]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Email') + '</div></td><td><div class="datagrid-cell"><a href="mailto:' + v[3] + '">' + v[3] + '</a></div></td></tr>';
            if (v[4]) html += '<tr class="datagrid-row"><td><div class="datagrid-cell">' + getTranslation('Mobile') + '</div></td><td><div class="datagrid-cell"><a href="callto:' + v[4] + '">' + v[4] + '</a></div></td></tr>';
        });
    }
    html += '</table></div></td></tr>';
    html += '<tr><td valign="bottom" align="center">';
    html += '<div id="member_details_buttons_' + current_uid + '"" style="dislay:none;">';
    html += $('#member_details_buttons').html();
    html += '</div>';
    html += '</td></tr></table>';
    return html;
}
function doMembersTree() {
    var tmp = new Array();
    tmp.push({
        'id': 0,
        'iconCls': 'icon-mmb-first',
        'text': getTranslation('First Level')
    });
    tmp.push({
        'id': '0_1',
        'iconCls': 'icon-mmb-all',
        'text': getTranslation('All Levels')
    });
    if (checkUserPermission('member_user_maratory', 'view')) {
        tmp.push({
            'id': 'b',
            'iconCls': 'icon-mmb-lock',
            'text': getTranslation('Blocked Out')
        });
    }
    //init id
    $.each(users_first,
    function(i, obj) {
        if (obj) {
            users_parents[i] = 0;
            users_subs[i] = obj.c;
            users_names[i] = obj.n;
            if (obj.c > 0) {
                name = obj.n + ' (' + obj.c + ')';
                tmp.push({
                    'id': obj.i,
                    'text': name,
                    'iconCls': 'icon-group',
                    'state': 'closed'
                });
            } else {
                name = obj.n;
                if (obj.t > 10) {
                    tmp.push({
                        'id': obj.i,
                        'iconCls': 'icon-mmb-dec',
                        'text': name
                    });
                } else {
                    tmp.push({
                        'id': obj.i,
                        'iconCls': 'icon-mmb-simple',
                        'text': name
                    });
                }
            }
        }
    });
    $('#members_tree').tree('loadData', tmp);
}